package marchsoft.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import marchsoft.exception.BadRequestException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName MailUtils
 * @Author ZhuGuangLiang <786945363@qq.com>
 * @Date 2022/03/28 22:23
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MailUtils {
	private final JavaMailSender mailSender;
	private final TemplateEngine templateEngine;
	@Value("${spring.mail.username}")
	private String from;
	@Value("${spring.mail.personal}")
	private String personal;

	public void sendSimpleMail(String to, String subject, String content) {
		MimeMessage message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(new InternetAddress(from, personal, "UTF-8"));
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(content, true);
			mailSender.send(message);
		} catch (UnsupportedEncodingException | MessagingException e) {
			throw new BadRequestException("邮箱发送失败，请重新发送");
		}
	}

	public void sendSimpleMail(String to, String subject, String content, String... cc) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);
		message.setTo(to);
		message.setCc(cc);
		message.setSubject(subject);
		message.setText(content);
		mailSender.send(message);
	}

	public void sendHtmlMail(String to, String subject, String template, Map<String, Object> content, String... cc) {
		Context context = new Context();
		for (Map.Entry<String, Object> entry : content.entrySet()) {
			context.setVariable(entry.getKey(), entry.getValue());
		}
		String templateContent = templateEngine.process(template, context);
		MimeMessage message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(new InternetAddress(from, personal, "UTF-8"));
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(templateContent, true);
			if (cc.length > 0) {
				helper.setCc(cc);
			}
			mailSender.send(message);
		} catch (MessagingException | UnsupportedEncodingException | MailSendException e) {
			throw new BadRequestException("发送失败");
		}
	}

	private Set<String> getInvalidAddress(MailSendException e) {
		Set<String> mails = new HashSet<>();
		for (Exception exception : e.getFailedMessages().values()) {
			if (exception instanceof SendFailedException) {
				for (Address address : ((SendFailedException) exception).getInvalidAddresses()) {
					mails.add(address.toString());
				}
			}
		}
		return mails;
	}

}
